package com.haoshuang.zhouzhoubang.ui.page.message

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.haoshuang.zhouzhoubang.databinding.FragmentMessageBinding
import com.haoshuang.zhouzhoubang.ui.adapter.ViewPagerAdapter
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.haoshuang.zhouzhoubang.ui.page.main.MainActivity
import com.haoshuang.zhouzhoubang.ui.page.message.child.error.ErrorMessageFragment
import com.haoshuang.zhouzhoubang.ui.page.message.child.order.OrderMessageFragment
import com.haoshuang.zhouzhoubang.ui.page.message.child.system.SystemMessageFragment
import com.haoshuang.zhouzhoubang.ui.page.message.child.team.TeamMessageFragment
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.visibleOrGone


/**
 *
 */
class MessageFragment : BaseVMFragment<MessageViewModel, FragmentMessageBinding>() {

    companion object {
        fun getInstance(): MessageFragment {
            val fragment = MessageFragment()
            fragment.arguments = Bundle().apply {

            }
            return fragment
        }
    }

    private val titles = arrayListOf("系统", "门店", "订单", "异常")

    private val fragments = arrayListOf<Fragment>()
    private var index = 0
    fun showIndex(index: Int){
        binding.viewPager.post {
            binding.viewPager.currentItem = index
        }
    }
    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "消息"
        binding.tvAllRead.clickWithTrigger {
            // 点击设置全部已读
            XPopup.Builder(mActivity)
                .asCustom(ConfirmDialog(
                    context = mActivity,
                    title = "温馨提示",
                    content = "确认将${when(index){
                        0 -> "系统"
                        1 -> "门店"
                        2 -> "订单"
                        else -> "异常"
                    }}消息标为已读",
                    cancelStr = "取消",
                    confirmStr = "确定",
                    isHideCancel = false,
                    isHideAgain = false,
                    onCancel = {},
                    onConfirm = {
                        vm.doMessageRead(index + 1)
                    }))
                .show()

        }
        initViewPager()
    }

    private fun initViewPager() {
        fragments.add(SystemMessageFragment.getInstance())
        fragments.add(TeamMessageFragment.getInstance())
        fragments.add(OrderMessageFragment.getInstance())
        fragments.add(ErrorMessageFragment.getInstance())

        val adapter = ViewPagerAdapter(childFragmentManager, lifecycle, fragments)
        binding.viewPager.adapter = adapter
        binding.viewPager.isUserInputEnabled = false
        binding.viewPager.offscreenPageLimit = 4
        binding.slidingTabLayout.setViewPager2(binding.viewPager, titles)

        binding.viewPager.registerOnPageChangeCallback(object : OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                index = position
                // 获取当前页面是否有未读消息
            }
        })


    }

    /**
     * 设置未读按钮
     */
    fun setUnReadButton(index: Int, hasUnRead: Boolean){
        if (index == this.index){
            binding.tvAllRead.visibleOrGone(hasUnRead)
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getMessageCount()
    }

    override fun initData() {

    }

    override fun lazyLoadData() {

    }

    fun refreshData(){
        vm.getMessageCount()
    }

    override fun startObserve() {
        vm.doMessageReadData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                when(index){
                    0 -> {
                        (fragments[0] as SystemMessageFragment).setMessageRead()
                    }
                    1 -> {
                        (fragments[1] as TeamMessageFragment).setMessageRead()
                    }
                    2 -> {
                        (fragments[2] as OrderMessageFragment).setMessageRead()
                    }
                    3 -> {
                        (fragments[3] as ErrorMessageFragment).setMessageRead()
                    }
                }
                vm.getMessageCount()

            }
        }
        vm.getMessageCountData.observeState(this) {
            onError { code, msg ->
            }
            onSuccess {
                dismissLoading()
                val data = it?.data
                if (data?.system_msg?.count == 0) {
                    binding.slidingTabLayout.hideMsg(0)
                } else {
                    binding.slidingTabLayout.showMsg(0, data?.system_msg?.count ?: 0)
                    binding.slidingTabLayout.setMsgMargin(0, 15f, 5f)
                }
                if (data?.team_msg?.count == 0) {
                    binding.slidingTabLayout.hideMsg(1)
                } else {
                    binding.slidingTabLayout.showMsg(1, data?.team_msg?.count ?: 0)
                    binding.slidingTabLayout.setMsgMargin(1, 15f, 5f)
                }
                if (data?.order_msg?.count == 0) {
                    binding.slidingTabLayout.hideMsg(2)
                } else {
                    binding.slidingTabLayout.showMsg(2, data?.order_msg?.count ?: 0)
                    binding.slidingTabLayout.setMsgMargin(2, 15f, 5f)
                }
                if (data?.feedback_msg_count?.count == 0) {
                    binding.slidingTabLayout.hideMsg(3)
                } else {
                    binding.slidingTabLayout.showMsg(3, data?.feedback_msg_count?.count ?: 0)
                    binding.slidingTabLayout.setMsgMargin(3, 15f, 5f)
                }

                val totalCount = (data?.system_msg?.count ?: 0) + (data?.team_msg?.count
                    ?: 0) + (data?.order_msg?.count ?: 0) + (data?.feedback_msg_count?.count ?: 0)
                if (activity is MainActivity) {
                    (activity as MainActivity).setMessageUnReadCount(totalCount)
                }
            }
        }
    }

}